package com.jzy.common.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.jzy.common.utils.Global;

/**
 * 
 * 类描述： 自定义springboot配置
 * 创建者： jiaozy
 * 项目名称： ssm
 * 创建时间： 2019年3月12日 下午5:50:58
 * Email：jzy19890914@gmail.com
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {

	//跨域支持 addCorsMappings
	//添加静态资源 addResourceHandlers
	
	//视图跳转控制器
	@Override
	public void addViewControllers(ViewControllerRegistry registry) {
		registry.addViewController("/").setViewName("forward:/login/showLogin");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
		WebMvcConfigurer.super.addViewControllers(registry);
	}

	//配置消息转换器 fastjson替换
	@Override
	public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
		
		WebMvcConfigurer.super.configureMessageConverters(converters);
		// 创建FastJson消息转换器
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        // 创建配置类
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setCharset(Global.UTF_8_CHARSET);
        // 修改配置返回内容的过滤
        //JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd hh:mm:ss";//WriteDateUseDateFormat配置时启用 
        fastJsonConfig.setSerializerFeatures(
            SerializerFeature.PrettyFormat
            //,SerializerFeature.WriteDateUseDateFormat //全局修改日期格式,默认为false
        );
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
         
        List<MediaType> fastMediaType = new ArrayList<>();
        fastMediaType.add(MediaType.APPLICATION_JSON_UTF8);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaType);
        // 将FastJson添加到视图消息转换器列表内
        converters.add(0, fastJsonHttpMessageConverter);
	}

	
}
